package edu.ccut.saturn.studio.export.excel;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnProjectUtil;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnSWTUtils;
import edu.ccut.saturn.studio.core.util.wizard.SaturnWizardPage;
import edu.ccut.saturn.studio.naviagtor.wizard.project.SaturnProjectNature;

/**
 * 项目统计报告向导页
 * 
 * @author 杨艳斌
 * 
 * @version 1.1 2009.12.17
 */

public class ExportReportWizardpage extends SaturnWizardPage {

	private Text text;
	private String saveDirectory;
	private Table table;
	private ISelection selection;
	private List<IProject> projects;
	private CheckboxTableViewer projectTableViewer;
	public static final String CLASSPATH_IS_TYPE_SRC = "src";
	public static final String DEFAULT_PATH = "D:/";
	public static final String SATURN_EXPORTWZARD_FILENAME = "exportFileName";

	public ExportReportWizardpage(ISelection selection) {
		setTitle(Messages.getString("ExportReportWizardpage.exportProjectReport"));  //$NON-NLS-1$
		this.selection = selection;
	}

	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		final GridLayout gridLayout = new GridLayout();
		container.setLayout(gridLayout);
		this.setControl(container);

		final Composite composite_1 = new Composite(container, SWT.NONE);
		final GridData gd_composite_1 = new GridData(500, 244);
		composite_1.setLayoutData(gd_composite_1);
		final GridLayout gridLayout_1 = new GridLayout();
		composite_1.setLayout(gridLayout_1);

		final Composite composite_inner = new Composite(composite_1, SWT.NONE);
		composite_inner.setLayout(new GridLayout());

		final Label label = new Label(composite_1, SWT.NONE);
		label.setLayoutData(new GridData(SWT.DEFAULT, 20));
		label.setText(Messages
				.getString("ExportReportWizardpage.chooseProject")); //$NON-NLS-1$
		final TableViewer tableViewer = new TableViewer(composite_1,
				SWT.FULL_SELECTION | SWT.BORDER | SWT.CHECK);
		table = tableViewer.getTable();
		table.setLayoutData(new GridData(453, 176));

		projectTableViewer = new CheckboxTableViewer(table);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		final TableColumn projectname = new TableColumn(table, SWT.NONE);
		projectname.setText(Messages
				.getString("ExportReportWizardpage.projectName")); //$NON-NLS-1$
		projectname.setWidth(470);
		projectname.setImage(Activator.getImage(Images.PROJECT_NAME));

		tableViewer
				.setContentProvider(new SaturnSWTUtils.SaturnTableViewerDefaultContentProvider());
		tableViewer
				.setLabelProvider(new SaturnSWTUtils.SaturnTableViewerDefaultLabelProvider() {

					public String getColumnText(Object obj, int index) {
						return ((IProject) obj).getName();
					}
				});

		projects = findProjectsMap();
		Collections.sort(projects, new Comparator<IProject>() {
			public int compare(IProject o1, IProject o2) {
				return o1.getName().compareTo(o2.getName());
			}
		});

		tableViewer.setInput(projects);

		final Composite composite_2 = new Composite(container, SWT.NONE);
		final GridData gd_composite_2 = new GridData(SWT.DEFAULT, 31);
		composite_2.setLayoutData(gd_composite_2);
		final GridLayout gridLayout_2 = new GridLayout();
		gridLayout_2.numColumns = 2;
		composite_2.setLayout(gridLayout_2);

		final Button button_all = new Button(composite_2, SWT.NONE);
		button_all.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				TableItem items[] = tableViewer.getTable().getItems();
				for (int i = 0; i < items.length; i++) {
					items[i].setChecked(true);
				}
			}
		});
		final GridData gd_button_all = new GridData(52, 24);
		button_all.setLayoutData(gd_button_all);
		button_all.setText(Messages.getString("ExportReportWizardpage.SelectAll")); //$NON-NLS-1$

		final Button button_reverse = new Button(composite_2, SWT.NONE);
		button_reverse.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				TableItem items[] = tableViewer.getTable().getItems();
				for (int i = 0; i < items.length; i++) {
					if (items[i].getChecked()) {
						items[i].setChecked(false);
					} else {
						items[i].setChecked(true);
					}
				}
			}
		});
		final GridData gd_button_reverse = new GridData(54, 24);
		button_reverse.setLayoutData(gd_button_reverse);
		button_reverse.setText(Messages.getString("ExportReportWizardpage.Deselect")); //$NON-NLS-1$

		final Composite composite_3 = new Composite(container, SWT.NONE);
		final GridData gd_composite_3 = new GridData(SWT.LEFT, SWT.BOTTOM,
				false, false);
		gd_composite_3.heightHint = 34;
		gd_composite_3.widthHint = 528;
		composite_3.setLayoutData(gd_composite_3);
		final GridLayout gridLayout_3 = new GridLayout();
		gridLayout_3.numColumns = 3;
		composite_3.setLayout(gridLayout_3);

		final Label label_savePath = new Label(composite_3, SWT.NONE);
		final GridData gd_label_savePath = new GridData(SWT.LEFT, SWT.BOTTOM,
				false, false);
		gd_label_savePath.widthHint = 63;
		label_savePath.setLayoutData(gd_label_savePath);
		label_savePath.setText(Messages.getString("ExportWizardPage.SavePath")); //$NON-NLS-1$

		text = new Text(composite_3, SWT.BORDER);
		text.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				String savepath = text.getText().trim()
						+ "/" + Messages
								.getString("ExportReportWizardpage.projectReport"); //$NON-NLS-2$
				setSaveDirectory(savepath);
			}
		});
		text.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false));

		final Button button = new Button(composite_3, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				DirectoryDialog dialog = new DirectoryDialog(Display
						.getDefault().getShells()[0]);
				dialog.setText(Messages.getString("ExportWizardPage.Catalog")); //$NON-NLS-1$
				dialog.setMessage(Messages
						.getString("ExportWizardPage.ChooseOneCatalog")); //$NON-NLS-1$

				String dir = text.getText();
				dialog.setFilterPath(dir);

				dir = dialog.open();
				if (dir != null) {
					dir = dir.replace(File.separator, "/");
					String savepath = dir
							+ "/"
							+ Messages
									.getString("ExportReportWizardpage.projectReport"); //$NON-NLS-2$
					setSaveDirectory(savepath);
					text.setText(dir);
				}
			}
		});

		final GridData gd_button = new GridData(SWT.LEFT, SWT.BOTTOM, false,
				false);
		gd_button.widthHint = 39;
		button.setLayoutData(gd_button);
		button.setText(Messages.getString("ExportWizardPage.Broose")); //$NON-NLS-1$
		initvalue();
	}
	
	/**
	 * 初始化工程列表
	 * 
	 * @param 
	 * @return
	 */
	private void initvalue() {
		
		if (selection instanceof IStructuredSelection) {
			IProject project = SaturnProjectUtil
					.getProject((IStructuredSelection) selection);
			if (project.getName() != null) {
				for (IProject newproject : projects) {
					if (newproject.getName().equals(project.getName())) {
						projectTableViewer
								.setCheckedElements(new Object[] { project });
					}
				}
			}
		}
		text.setText(DEFAULT_PATH);
	}
	
	/**
	 * 获得工程列表
	 * 
	 * @param 
	 * @return
	 */
	private List<IProject> findProjectsMap() {
		
		List<IProject> selectedprojects = new ArrayList<IProject>();
		IProject projects[] = SaturnProjectUtil.getAllProjects();

		for (IProject project : projects) {
			try {
				if (project.isOpen()
						&& project
								.hasNature(SaturnProjectNature.PROJECT_NATURE)) {
					selectedprojects.add(project);
				}
			} catch (CoreException e) {
				e.printStackTrace();
			}
		}

		return selectedprojects;
	}

	protected void allProjectNames() {

	}

	@Override
	public void initalValues(Map<String, Object> values) {

	}

	public String getSaveDirectory() {
		return saveDirectory;
	}

	public void setSaveDirectory(String saveDirectory) {
		this.saveDirectory = saveDirectory;
	}
	
	/**
	 * 获得选择的工程列表
	 * 
	 * @param 
	 * @return List<IProject>
	 */
	public List<IProject> getProjects() {
		List<IProject> selecteds = new ArrayList<IProject>();

		if (projectTableViewer != null) {
			Object[] checkeds = projectTableViewer.getCheckedElements();
			for (Object checked : checkeds) {
				selecteds.add((IProject) checked);
			}
			return selecteds;
		}
		return null;
	}
}
